package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.CustomerService;
import com.xdtech.hotel.service.RoomService;
import com.xdtech.hotel.vo.CustomerItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:08:36
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	@Autowired
	private  RoomService roomService;
	@RequestMapping(value="/customer.do",params = "customer")
	public ModelAndView skipCustomer() {
		return new ModelAndView("hotel/customer/customer_ftl");
	}
	
	@RequestMapping(value="/customer.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(CustomerItem item,Pagination pg) {
		Map<String, Object> results =  customerService.loadPageDataByConditions(pg,item,"findCustomerByCondition");
		return results;
	}
	
	@RequestMapping(value="/customer.do",params = "editCustomer")
	public ModelAndView editCustomer(HttpServletRequest request,Long customerId) {
		if (customerId!=null) {
			request.setAttribute("customerItem", customerService.loadCustomerItem(customerId));
		}
		return new ModelAndView("hotel/customer/editCustomer_ftl");
	}
	
	@RequestMapping(value="/customer.do",params = "saveCustomer")
	@ResponseBody
	public ResultMessage saveCustomer(CustomerItem item) {
		ResultMessage r = new ResultMessage();
		if (customerService.saveOrUpdateCustomer(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/customer.do",params = "deleteCustomerItems")
	@ResponseBody
	public ResultMessage deleteCustomerItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> customerIds = new ArrayList<Long>();
			for (String id : tempIds) {
				customerIds.add(Long.valueOf(id));
			}
			customerService.deleteCustomerInfo(customerIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	/**
	 * 
	 * 跳转入住登记界面
	 * @author max.zheng
	 * @create 2015-4-18下午9:11:37
	 * @modified by
	 * @param request
	 * @param customerId
	 * @return
	 */
	@RequestMapping(value="/customer.do",params = "liveCustomer")
	public ModelAndView liveCustomer(HttpServletRequest request,Long roomId) {
		RoomItem roomItem = roomService.loadRoomItem(roomId);
		request.setAttribute("roomItem", roomItem);
		return new ModelAndView("hotel/customer/liveCustomer_ftl");
	}
	/**
	 * 
	 * 查看入住登记信息
	 * @author max.zheng
	 * @create 2015-4-18下午9:11:37
	 * @modified by
	 * @param request
	 * @param customerId
	 * @return
	 */
	@RequestMapping(value="/customer.do",params = "liveCustomerInfo")
	public ModelAndView liveCustomerInfo(HttpServletRequest request,Long roomId) {
		CustomerItem customerItem = customerService.loadLiveCustomerItemByRoomId(roomId);
		request.setAttribute("customerItem", customerItem);
		request.setAttribute("roomId", roomId);
		return new ModelAndView("hotel/customer/liveCustomerInfo_ftl");
	}
	
	/**
	 * 
	 * 入住登记
	 * @author max.zheng
	 * @create 2015-4-18下午9:25:12
	 * @modified by
	 * @return
	 */
	@RequestMapping(value="/customer.do",params = "stay")
	@ResponseBody
	public ResultMessage stay(CustomerItem item) {
		ResultMessage r = new ResultMessage();
		boolean rs = customerService.stay(item);
		r.setSuccess(rs);
		return r;
	}
	
	/**
	 * 
	 * 入住登记
	 * @author max.zheng
	 * @create 2015-4-18下午9:25:12
	 * @modified by
	 * @return
	 */
	@RequestMapping(value="/customer.do",params = "leave")
	@ResponseBody
	public ResultMessage leave(CustomerItem item) {
		ResultMessage r = new ResultMessage();
		boolean rs = customerService.leave(item);
		r.setSuccess(rs);
		return r;
	}

}
